<?php
/*
 * 
 * Implementeaza constructia elementelor de baza
 * 
 */

class xmlBasicElements {
	//error codes
	const INVPTH = "Invalid file path!";
	const INVEXT = "Invalid file name!";
	const INVROWNR="Invalid row number!";
	const INVLISTELEM="Invalid list element number!";
	const INVNOTECNT="Invalid note content!";
	const INVATTCNT="Invalid attention content!";

	/**
	 * DOMDocument
	 * @access private 
	 */
	private $dom;

	/**
	 * parintele noului element
	 */
	private $parentNode;

	/**
	 * constructor
	 * @return DOMElement 
	 */
	public function __construct(& $parent, & $dom) {
		$this->parentNode = & $parent;
		$this->dom = & $dom;
	}

	/**
	 * creare paragraf nou
	 * @param string $paraId id-ul paragrafului
	 * @param string $content
	 * @return bool TRUE if all ok
	 */
	public function createParagraph($paraId, $paraContent) {
		$newPara = $this->dom->createElement('paragraph', $paraContent);
		$newParaId = new DOMAttr('paraId', $paraId);
		$newPara->appendChild($newParaId);
		$this->parentNode->appendChild($newPara);
	}
	
	/**
	 * update paragraf
	 * @param string $paraId id-ul paragarfului ce va fi modificat
	 * @param string $paraContent noul continut
	 * @return bool TRUE if all ok
	 */
	public function updateParagraph($paraId, $paraContent){
		$oldParaList=$this->dom->getElementsByTagName('paragraph');
		//var_dump($this);
        foreach($oldParaList as $node){
            if($node->getAttribute('paraId') == $paraId ){
            	//echo("OK<br />");
            	$newPara=$this->dom->createElement('paragraph',$paraContent);
            	$newParaId= new DOMAttr('paraId', $paraId);
				$newPara->appendChild($newParaId);
				
				$this->parentNode->replaceChild($newPara, $node);
				return true;
            }
        }
	}

	/**
	 * returneaza tipul MIME pt imagini
	 * throws exceptions
	 * @access private
	 * @param string $imgSrc calea catre imagine
	 * @return string or FALSE if errors occur
	 */
	private function getImgMime($imgSrc) {
		$temp = explode('/', $imgSrc);
		$tempLength = count($temp);
		if ($tempLength == 0 || $tempLength == 1) {
			throw new Exception(self :: INVPTH);
			return false;
		}
		$imgName = $temp[$tempLength -1];
		$imgNameSplit = explode('.', $imgName);
		if (count($imgNameSplit) == 1) {
			throw new Exception(self :: INVEXT);
			return false;
		}
		$imgMime = $imgNameSplit[1];
		return $imgMime;
	}
	
	/**
	 * creare sectiune imagine
	 * throws exceptions
	 * @access public
	 * @param string $imgId atributul ID al imaginii
	 * @param string $imgSrc path-ul imaginii
	 * @return bool TRUE if no error occurs
	 */
	public function createImage($imgId, $imgSrc){
		$newImg=$this->dom->createElement('image');
		$newImgId=new DOMAttr('imgId',$imgId);
		$newImg->appendChild($newImgId);
		$newImgSrc=$this->dom->createElement('imgSrc',$imgSrc);
		$imgMime='';
		try{
			$imgMime=$this->getImgMime($imgSrc);
		}catch(exception $e){
			throw new Exception ($e->getmessage());
			return false;
		}
		$newImgMime=$this->dom->createElement('imgMIME',$imgMime);
		$newImg->appendChild($newImgSrc);
		$newImg->appendChild($newImgMime);
		$this->parentNode->appendChild($newImg);		
	}
	
	/**
	 * creare sectiune table
	 * throws exceptions
	 * @access public
	 * @param string $idTable id-ul campului table
	 * @param array[array] $tRows liniile tabelului
	 * @param array [$tHeader] headerul tabelului
	 * @param string [$tCaption] denumirea tabelului
	 * @return bool TRUE if no error occurs 
	 */
	public function createTable($idTable, $tRows, $tCaption='', $tHeader=''){
		$newTable=$this->dom->createElement('table');
		$newTableId=new DOMAttr('tbId',$idTable);
		$newTable->appendChild($newTableId);
		
		//caption
		if(strlen($tCaption)!=0){
			$newTableCaption=$this->dom->createElement('tableCaption',$tCaption);
			$newTable->appendChild($newTableCaption);
		}
		
		//header
		if(count($tHeader)!=0){
			$newTableHeader=$this->dom->createElement('tableHeader');
			
			$newTableHeaderTr=$this->dom->createElement('tableRow');
			$newTableHeaderTrIndex=new DOMAttr('rowIndex','0');
			$newTableHeaderTr->appendChild($newTableHeaderTrIndex);			
			foreach($tHeader as $th){
				$td=$this->dom->createElement('tableColumn',$th);
				$newTableHeaderTr->appendChild($td);
			}
			$newTableHeader->appendChild($newTableHeaderTr);
			$newTable->appendChild($newTableHeader);
		}
		
		//rows
/*		if(count($tRows)==0){
			throw new Exception (self::INVROWNR);
			return false;
		}else{
			foreach($tRows as $tr){
				$newRow=$this->dom->createElement('tableRow');
				foreach($tr as $trc){
					$newColumn=$this->dom->createElement('tableColumn',$trc);
					$newRow->appendChild($newColumn);
				}
				$newTable->appendChild($newRow);
			}
		}*/
		
		$this->parentNode->appendChild($newTable);
		return true;
	}
	
	/**
	 * creare sectiune list
	 * throws exceptions
	 * @access public
	 * @param string $idList id-ul tagului list
	 * @param array $listElem elementele listei
	 * @param string $listType tipul listei default 'unordered'
	 * @return bool TRUE if no error occurs
	 */
	public function createList($idList, $listElem, $listType='unordered'){
		$newList=$this->dom->createElement('list');
		$newListId=new DOMAttr('listId',$idList);
		$newListType=new DOMAttr('listType',$listType);
		$newList->appendChild($newListId);
		$newList->appendChild($newListType);
		
		if(count($listElem)==0){
			throw new Exception (self::INVLISTELEM);
			return false;
		}else{
			foreach($listElem as $le){
				$newListElement=$this->dom->createElement('listElement',$le);
				$newList->appendChild($newListElement);
			}
		}
		$this->parentNode->appendChild($newList);
		return true;
	} 
	
	/**
	 * creare sectiune note
	 * throws exceptions
	 * @access public
	 * @param $string $noteId id-ul tagului note
	 * @param $string $noteContent
	 * @return bool TRUE if no error occurs
	 */
	public function createNote($noteId, $noteContent){
		$newNote=$this->dom->createElement('note');
		$newNoteId=new DOMAttr('noteId',$noteId);
		$newNote->appendChild($newNoteId);
		
		if(strlen($noteContent)==0){
			throw new Exception (self::INVNOTECNT);
			return false;
		}else{
			$newNoteText=new DOMText($noteContent);
			$newNote->appendChild($newNoteText);
		}
		
		$this->parentNode->appendChild($newNote);
		return true;
	}
	
	/**
	 * creare sectiune attention
	 * throws exceptions
	 * @access public
	 * @param $string $attId id-ul tagului attention
	 * @param $string $attContent
	 * @return bool TRUE if no error occurs
	 */
	public function createAttention($attId, $attContent){
		$newAtt=$this->dom->createElement('attention');
		$newAttId=new DOMAttr('attnId',$attId);
		$newAtt->appendChild($newAttId);
		
		if(strlen($attContent)==0){
			throw new Exception (self::INVATTCNT);
			return false;
		}else{
			$newAttText=new DOMText($attContent);
			$newAtt->appendChild($newAttText);
		}
		
		$this->parentNode->appendChild($newAtt);
		return true;
	}
	
	
	/**
	 * creare sectiune quote
	 * throws exceptions
	 * @access public
	 * @param $string $quoteId id-ul tagului quote
	 * @param $string $quoteContent
	 * @return bool TRUE if no error occurs
	 */
	public function createQuote($quoteId, $quoteContent){
		$newQuote=$this->dom->createElement('quote');
		$newQuoteId=new DOMAttr('quoteId',$quoteId);
		$newQuote->appendChild($newQuoteId);
		
		if(strlen($quoteContent)==0){
			throw new Exception (self::INVATTCNT);
			return false;
		}else{
			$newQuoteText=new DOMText($quoteContent);
			$newQuote->appendChild($newQuoteText);
		}
		
		$this->parentNode->appendChild($newQuote);
		return true;
	}
}
?>